﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;

class ParseAnimData
{
    Dictionary<string, AnimTransformData> AnimTransformDatas = new Dictionary<string, AnimTransformData>();
    static ParseAnimData instance;
    public static ParseAnimData Instance
    {
        get
        {
            if (instance == null)
                instance = new ParseAnimData();
            return instance;
        }
    }
    public AnimTransformData Parse(string AnimName)
    {
        string path = AnimConfig.folderPath + "/" + AnimName + "_trans.json";
        if (File.Exists(path))
        {
            if (!AnimTransformDatas.ContainsKey(AnimName))
            {
                string json = File.ReadAllText(path);
                var animTransformData = JsonUtility.FromJson<AnimTransformData>(json);
                AnimTransformDatas.Add(AnimName, animTransformData);
                Debug.Log("解析出数据" + animTransformData.length);
                return animTransformData;
            }
            else
            {
                return AnimTransformDatas[AnimName];
            }
        }
        else
        {
            return null;
        }
    }
    public Vector3 GetPosByTime(string anim, float time)
    {
        var animTransformData = Parse(anim);
        if (animTransformData != null)
            return animTransformData.GetRealPosition(time);
        return Vector3.zero;
    }
    public Vector3 GetRotByTime(string anim, float time)
    {
        var animTransformData = Parse(anim);
        if (animTransformData != null)
            return animTransformData.GetRealRotation(time);
        return Vector3.zero;
    }
}